package vn.com.shoppie.activity;

import java.util.ArrayList;

import vn.com.shoppie.setting.SettingPreference;
import vn.com.shoppie.sobject.User;
import vn.com.shoppie.sobjmng.UserMng;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class WelcomeActivity extends FragmentActivity{
	Animation welcomeAnim;
	Animation welcomeAnimText;
	TextView tvLogo;
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_welcome);
		welcomeAnim=AnimationUtils.loadAnimation(this, R.anim.shoppie_welcome_anim);
		welcomeAnimText=AnimationUtils.loadAnimation(this, R.anim.shoppie_welcome_anim_text);
		
		tvLogo=(TextView)findViewById(R.id.tv_logo);
		tvLogo.setVisibility(View.INVISIBLE);
		
		welcomeAnim.setAnimationListener(new AnimationListener() {
			
			public void onAnimationStart(Animation arg0) {
				
			}
			
			public void onAnimationRepeat(Animation arg0) {
				
			}
			
			public void onAnimationEnd(Animation arg0) {
				tvLogo.setVisibility(View.VISIBLE);
				tvLogo.setAnimation(welcomeAnimText);
				welcomeAnimText.startNow();
			}
		});
		
		
		ImageView logo=(ImageView)findViewById(R.id.iv_logo);
		
		logo.setAnimation(welcomeAnim);
		
		
		welcomeAnimText.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation arg0) {
				
			}
			
			@Override
			public void onAnimationRepeat(Animation arg0) {
				
			}
			
			@Override
			public void onAnimationEnd(Animation arg0) {
				animationFinish();
			}
		});
		welcomeAnim.startNow();
		
		
		// background processing
	}
	protected void animationFinish() {
		UserMng uMng=new UserMng(this);
		ArrayList<User> users=uMng.select();
		if(SettingPreference.getFirstUse(this)
				|| users==null 
				|| users.isEmpty()){
			// show dialog register
			startShoppie();
		}else{
			startShoppie();
		}
		
	}
	protected void startShoppie() {
		startActivity(new Intent(getApplicationContext(), MainActivity.class));
		WelcomeActivity.this.finish();
	}
	static Toast mToast;
	public void showToast(String text){
		if(mToast==null) mToast=Toast.makeText(this, text, Toast.LENGTH_SHORT);
		mToast.setText(text);
		mToast.cancel();
		mToast.show();
	}
}
